<?php
/**
 * 	Singleton assurant qu'il n'y ait qu'une seule instance de MyPDO dans toute l'application, permet d'effectuer toutes les actions basiques possibles avec MyPDO
 */
class SingletonPDO {
	private $my_pdo = null;
	private static $my_pdo_instance = null;

	private function __construct()
	{
		try {
			$this->my_pdo = new MyPDO(CFG_DB_DSN, USER_DB, MDP_BD,array(
			    PDO::ATTR_AUTOCOMMIT=>false,
			    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
			    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
			    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
			));
		}
		catch(PDOException $e) {
			throw new Exception("Erreur de connexion PDO", 0, $e);
		}
	}

	public static function getInstance(){
		if (is_null(self::$my_pdo_instance)){
			self::$my_pdo_instance = new SingletonPDO();
		}
		return self::$my_pdo_instance;
	}

	public function prepare($query){
		global $cpt_req;
		$cpt_req++;
		return $this->my_pdo->prepare($query);
	}

	public function query($query){
		try {
			return $this->my_pdo->query($query);
		}
		catch(PDOException $e) {
			echo $query;
			throw new Exception("PDO query error : ". $e->getMessage());
		}
	}

	public function exec($query){
		return $this->my_pdo->exec($query);
	}

	public function lastInsertId(){
		return $this->my_pdo->lastInsertId();
	}

	public function beginTransaction(){
		return $this->my_pdo->beginTransaction();
	}

	public function inTransaction(){
		return $this->my_pdo->inTransaction();
	}

	public function rollBack(){
		return $this->my_pdo->rollBack();
	}

	public function commit(){
		return $this->my_pdo->commit();
	}
}
?>